package com.rjhch.tonglink.server.jms.mbd.remote;

import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.Properties;

public class TongWebClient {
	Context ctx = null;
	Connection conn = null;
	Session session = null;
	Queue queue = null;
	private void createJNDI() throws Exception{
		Properties pro = new Properties();
		pro.setProperty("java.naming.factory.initial", "com.tongweb.naming.SerialInitContextFactory");
		pro.setProperty("java.naming.factory.url.pkgs", "com.tongweb.naming");
		pro.setProperty("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
		ctx = new InitialContext(pro);
		
		QueueConnectionFactory connectionFactory = (QueueConnectionFactory)ctx.lookup("RemoteQueueConnectionFactory");
		conn = connectionFactory.createConnection();
		session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
		queue = (Queue)ctx.lookup("lq1");
	}
	
	private void sendMsg(String s) throws Exception{
		MessageProducer producer = session.createProducer(queue);
		TextMessage  tm = session.createTextMessage(s);
		producer.send(tm);
		producer.close();
	}

	
	private void closeAll() throws Exception{
		if(session != null){
			session.close();
		}
		if(conn != null){
			conn.close();
		}
	}
	
	public static void main(String[] args) throws Exception{
		TongWebClient twc = new TongWebClient();
		twc.createJNDI();
		twc.sendMsg("test send a TextMessage ... ");
		twc.closeAll();
	}
}
